# coding:utf-8
# 执行前，统一的前置处理

from util.operation_excel import OperationExcel
import data_config
from util.operation_json import OperetionJson,Path
# from util.connect_db import OperationMysql
class GetData:
	def __init__(self):
		self.opera_excel = OperationExcel()

	#去获取excel行数,就是我们的case个数	
	def get_case_lines(self):
		return self.opera_excel.get_lines()

	#获取是否执行
	def get_is_run(self,row):
		flag = None
		col = int(data_config.get_run())
		run_model = self.opera_excel.get_cell_value(row,col)
		if run_model == 'yes':
			flag = True
		else:
			flag = False
		return flag

	#是否携带cookie
	def is_cookie(self,row):
		col = int(data_config.get_cookie())
		cookie = self.opera_excel.get_cell_value(row,col)
		if cookie != '':
			return cookie
		else:
			return None

	#获取请求方式
	def get_request_method(self,row):
		col = int(data_config.get_run_way())
		request_method = self.opera_excel.get_cell_value(row,col)
		return request_method

	#获取url
	def get_request_url(self,row):
		col = int(data_config.get_url())
		url = self.opera_excel.get_cell_value(row,col)
		return url

	# 获取excel的data关键字
	def get_request_data(self,row):
		col = int(data_config.get_data())
		data = self.opera_excel.get_cell_value(row,col)
		if data == '':
			return None
		return data

	# 通过data关键字,拿到data的json数据
	def get_data_for_json(self,row):
		# data文件是默认参数所有不需要传参
		opera_json = OperetionJson()
		request_data = opera_json.get_data(self.get_request_data(row))
		return request_data


	# 获取excel的header关键字
	def get_header_data(self, row):
		col = int(data_config.get_header())
		header = self.opera_excel.get_cell_value(row, col)
		if header == '':
			return None
		return header

	# 通过header关键字拿到data的json数据
	def get_header_for_json(self,row):
		# header文件,需要另外传参
		opera_json = OperetionJson(Path.path_header)
		request_header = opera_json.get_data(self.get_header_data(row))
		return request_header

	# 获取excel中的header_type
	def get_header_type(self,row):
		col = int(data_config.get_header_type())
		header_type = self.opera_excel.get_cell_value(row,col)
		if header_type == '':
			return None
		return header_type



	#获取预期结果
	def get_expcet_data(self,row):
		col = int(data_config.get_expect())
		expect = self.opera_excel.get_cell_value(row,col)
		if expect == '':
			return None
		return expect

	# #通过sql获取预期结果
	# def get_expcet_data_for_mysql(self,row):
	# 	op_mysql = OperationMysql()
	# 	sql = self.get_expcet_data(row)
	# 	res = op_mysql.search_one(sql)
	# 	return res.decode('unicode-escape')

	def write_result(self,row,value):
		col = int(data_config.get_result())
		self.opera_excel.write_value(row,col,value)

	#获取依赖数据的key
	def get_depend_key(self,row):
		col = int(data_config.get_data_depend())
		depent_key = self.opera_excel.get_cell_value(row,col)
		if depent_key == "":
			return None
		else:
			return depent_key

	#判断是否有case依赖
	def is_depend(self,row):
		col = int(data_config.get_case_depend())
		depend_case_id = self.opera_excel.get_cell_value(row,col)
		if depend_case_id == "":
			return None
		else:
			return depend_case_id

	# 获取数据依赖字段
	def get_depend_field(self,row):
		col = int(data_config.get_field_depend())
		data = self.opera_excel.get_cell_value(row,col)
		if data == "":
			return None
		else:
			# 将excel上的依赖key分割，并转格式为int（if 可以转的话）
			# 如 products.0.quantity
			excel_s = data.split(".")
			# print (excel_s)
			excel_int_list = []
			for a in excel_s:
				try:
					b = int(a)
				except:
					c = a
				else:
					c = b
				excel_int_list.append(c)
			return excel_int_list



